﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pb;

public class ForgetPsdModel : MonoBehaviour {

    public GameObject ReturnBtn;
    public GameObject ModifyBtn;
    public GameObject VerificationCodeBtn;
    //public UIInput phoneNumInput;
    //public UIInput VerificationCodeInput;

    //要修改的手机号
    public UIInput UserPhone;
    //验证码
    public UIInput Yanzhengma;
    //新密码
    public UIInput newPasswardInput;

    public UILabel label;
    public UISprite sprite;

    EginUser user = EginUser.Instance;

    void Awake()
    {
        UIEventListener.Get(ReturnBtn).onClick = ButtonClick;
        UIEventListener.Get(ModifyBtn).onClick = ButtonClick;
        UIEventListener.Get(VerificationCodeBtn).onClick = ButtonClick;

        label = VerificationCodeBtn.transform.GetChild(0).GetComponent<UILabel>();
        sprite = VerificationCodeBtn.GetComponent<UISprite>();
    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "ReturnBtn")
        {
            Destroy(gameObject);
        }
        else if (button.name == "ModifyBtn")
        {
            if (UserPhone.value.Length != 0 && Yanzhengma.value.Length != 0 && newPasswardInput.value.Length != 0)
            {
                string newPsd = ProtoDataToMsg.stringToUTF8(DataSet.StringToMd5(newPasswardInput.value));
                //发送修改密码消息
                FindPasswordRequest lg = new FindPasswordRequest { Telephone = UserPhone.value, ValidCode = int.Parse(Yanzhengma.value), NewPasswd = newPsd };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(lg, MsgId.FindPasswordRequest);
            }
            else
            {
                user.logMsg = "请输入完整信息";
                GameObject Set = NGUITools.AddChild(gameObject, (GameObject)(Resources.Load("LogMsg")));
            }

        }
        else if (button.name == "VerificationCodeBtn")
        {
            if (UserPhone.value.Length != 0)
            {
                //请求验证码
                TelephoneCodeRequest lg = new TelephoneCodeRequest { Telephone = UserPhone.value, CodeType = TelephoneCodeType.FindPassword };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(lg, MsgId.TelephoneCodeRequest);
                CountDown("yzm2", true);
            }
            else
            {
                user.logMsg = "请输入完整信息";
                GameObject Set = NGUITools.AddChild(gameObject, (GameObject)(Resources.Load("LogMsg")));
            }

        }
    }

    /// <summary>
    /// 倒计时
    /// </summary>
    /// <param name="time"></param>
    /// <param name="name"></param>
    void CountDown(string name, bool isShow)
    {
        print("执行了");
        sprite.spriteName = name;
        if (isShow)
        {
            label.gameObject.SetActive(true);
            VerificationCodeBtn.GetComponent<BoxCollider>().enabled = false;
            StartCoroutine(reduceTime(60, label));
        }
        else
        {
            VerificationCodeBtn.GetComponent<BoxCollider>().enabled = true;
            label.gameObject.SetActive(false);
        }

    }

    IEnumerator reduceTime(int time, UILabel label)
    {
        label.text = time.ToString();
        while (time > 0)
        {
            yield return new WaitForSeconds(1);
            time--;
            if (time <= 0)
            {
                CountDown("yzm1", false);
                yield break;
            }
            label.text = time.ToString();
        }
    }

}
